package com.zfsoft.deployui.controller.deploy;

import com.zfsoft.deployui.deploy.Host;
import com.zfsoft.deployui.deploy.ResultInfo;
import com.zfsoft.deployui.enums.ResponseCodeEnum;
import com.zfsoft.deployui.service.AddHostServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/deploy")
public class HostDeploy {
    @Autowired
    private AddHostServiceImpl addHostService;

    List<Host> existHost=new ArrayList<>();

    @RequestMapping("/hostList")
    @ResponseBody
    public List<Host> hostList(){
        for (int i = 0; i < addHostService.queryAll().size(); i++) {
            existHost.add(addHostService.queryAll().get(i));
        }
        HashSet tmp=new HashSet(existHost);
        existHost.clear();
        existHost.addAll(tmp);
        System.out.println(addHostService.queryAll());
        return addHostService.queryAll();
    }

    @RequestMapping("/hostListByStatus")
    @ResponseBody
    public List<Host> hostListByStatus(){

        for (int i = 0; i < addHostService.queryByStatus("0").size(); i++) {
            System.out.println(i);
//            existHost.add(addHostService.queryAll().get(i));
        }
//        HashSet tmp=new HashSet(existHost);
//        existHost.clear();
//        existHost.addAll(tmp);
        System.out.println(addHostService.queryByStatus("0"));
        return addHostService.queryByStatus("0");
    }

    @RequestMapping(value = "/addHost",method = RequestMethod.POST)
    public ResultInfo addHost(@RequestBody Host host) throws UnsupportedEncodingException {
        System.out.println("host = " + host);
        for (int i = 0; i < existHost.size(); i++) {
            if (addHostService.findHost(host.getIp())!=null || addHostService.findHost(host.getMark())!=null){
                return new ResultInfo(ResponseCodeEnum.FAILURE, "不能添加重复的mark或者ip");
            }
        }
        try{
            addHostService.addHost(host);
        }catch (Exception e){
            return new ResultInfo(ResponseCodeEnum.FAILURE, e.getMessage());
        }
        String defaultCharsetName=Charset.defaultCharset().displayName();
        System.out.println("defaultCharsetName:"+defaultCharsetName);
        return new ResultInfo(ResponseCodeEnum.SUCCESS, "添加主机成功");
    }

    @RequestMapping(value = "/alterHost",method = RequestMethod.POST)
    public ResultInfo alterHost(@RequestBody Host host){
        System.out.println("host = " + host);
        try {
            addHostService.alterHost(host);
        }catch (Exception e){
            return new ResultInfo(ResponseCodeEnum.FAILURE, e.getMessage());
        }
        return new ResultInfo(ResponseCodeEnum.SUCCESS, "修改主机成功");
    }

    @RequestMapping(value = "/delHost",method = RequestMethod.POST)
    public ResultInfo delHost(@RequestBody  Host host){
        System.out.println("ip=:" + host.getIp());
        try {
            addHostService.delHost(host.getIp());
            Iterator<Host> sListIterator = existHost.iterator();
            while(sListIterator.hasNext()){
                Host e = sListIterator.next();
                if(e.getIp().equals(host.getIp())){
                    sListIterator.remove();
                }
            }
        }catch (Exception e){
            return new ResultInfo(ResponseCodeEnum.FAILURE, e.getMessage());
        }
        return new ResultInfo(ResponseCodeEnum.SUCCESS, "删除主机成功");
    }
}
